<?php
namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use fast\Tree;

/**
 * WanlShop文章接口
 */
class ArticleComment extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wanlshop\ArticleComment;
    }

    /**
     * 获取指定文章列表
     *
     * @ApiSummary  (WanlShop 获取文章评价列表)
     * @ApiMethod   (POST)
     *
     * @param string $id 发现ID
     */
    public function getList()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $article_id = $this->request->post('article_id');
            $where['article_id'] = $article_id;
            $data = $this->model
                ->alias('c')
                ->join('user u','c.user_id=u.id','LEFT')
                ->where($where)
                ->field('c.*,u.avatar,u.nickname,u.level')
                ->order('id desc')
                ->paginate();

            $this->success('返回成功', $data);
        }
        $this->error(__('非法请求'));

    }


    /**
     * 文章发表评论
     *
     * @ApiSummary  (WanlShop 文章发表评论)
     * @ApiMethod   (POST)
     *
     * @param string $id 文章ID
     */
    public function addComment()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $article_id = $this->request->post('article_id');
            $article_id ? $article_id : ($this->error(__('非法请求')));
            $content = $this->request->post('content');
//            $pid = $this->request->post('pid');
            // 内容审核
            $article = model('app\api\model\wanlshop\Article')
                ->where(['id' => $article_id])
                ->find();
            if(!$article){
                $this->error(__('作品不存在无法评论'));
            }
            $row = $this->model;
            $row->data([
//                'pid' => $pid ? $pid : 0,
                'article_id'  => $article['id'],
                'user_id'  => $this->auth->id,
                'content' => $content
            ]);
            if($row->save()){
                $article->setInc('comments');
                $this->success('ok');
            }else{
                $this->error(__('评论失败'));
            }
        }
        $this->error(__('非法请求'));
    }
	
}
